<script setup lang="ts">
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { provinceAndCityData } from 'element-china-area-data'
import {ElMessageBox} from 'element-plus'
import {onMounted, ref, watch} from "vue";
import {dictKeyToSelect} from "@/common/dict.ts";

const props = defineProps({
  jianli:{
    type: Array,
    required: true
  },
  jiangcheng:{
    type: Array,
    required: true
  },
  family:{
    type: Array,
    required: true
  },
  drawerData: {
    type: Object,
    required: true
  },
  drawerConfig: {
    type: Object,
    required: true
  },
  dict: {
    type: Object,
    required: false,
    default: {
      dictIndex: [],
      dictList: []
    }
  }
})



let form = ref({}) //表单
let locale = ref(zhCn)
onMounted(async () => {

})
watch(props.drawerConfig, () => {
  let tmp = {}
  if (props.drawerConfig.form) {
    props.drawerConfig.form.map(item => {
      tmp[item.key] = props.drawerData[item.key]
    })
  }
  form.value = props.drawerData
}, {deep: true})


/**
 * 是否显示该组件，根据配置type和condition条件分别判断
 * @param item
 * @param type
 */
const display = (item, type) => {
  if (item.type === type) {
    if (item.condition) {
      for (let i in item.condition) {
        let con = item.condition[i]
        if (con.type === 'equal' && con.value.indexOf(form.value[con.key]) == -1) return false
        if (con.type === 'noEqual' && con.value.indexOf(form.value[con.key]) != -1) return false
      }
      return true
    } else {
      return true
    }
  } else {
    return false
  }
}
const displayAlert = (item) => {
  if (item.alert) {
    if (item.condition) {
      for (let i in item.condition) {
        let con = item.condition[i]
        if (con.type === 'equal' && con.value.indexOf(form.value[con.key]) == -1) return false
        if (con.type === 'noEqual' && con.value.indexOf(form.value[con.key]) != -1) return false
      }
      return true
    } else {
      return true
    }
  } else {
    return false
  }

}


/**
 * 事件调度中心
 * @param eventName:string 事件名称
 * @param value:object 事件值
 */
const emit = defineEmits(['doit'])
const doit = (eventName: string, value = {},list={}) => {
  if(list.peopleResumeList){
    list.peopleResumeList.map(item=>{
      item.start = item.time[0]
      item.end = item.time[1]
      return item
    })
  }
  console.log(list,'list')
  emit('doit', {
    eventName: eventName,
    value: value,
    list
  })
}
// 添加简历
const addJianli = () => {
  props.jianli.push({
    time:['',''],
    remark:''
  })
}
// 删除简历
const delJianli = (index) => {
  ElMessageBox.confirm(
        '确定要删除吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
        .then(() => {
          props.jianli.splice(index,1)
        })
  
}
// 添加奖惩
const addJiangcheng = () => {
  props.jiangcheng.push({
    reward:'',
    rewardTime:''
  })
}
// 删除奖惩
const delJiangcheng = (index) => {
  ElMessageBox.confirm(
        '确定要删除吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
        .then(() => {
          props.jiangcheng.splice(index,1)
        })
  
}
// 添加成员
const addFamily = () => {
  props.family.push({
    appellation:'',
    age:'',
    outLook:'',
    jobRemark:'',
    name:''
  })
}
// 删除成员
const delFamily = (index) => {
  ElMessageBox.confirm(
        '确定要删除吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
        .then(() => {
          props.family.splice(index,1)
        })
  
}
//关闭drawer
const handleClose = () => {
  if (props.drawerConfig.messageBox.status) {
    ElMessageBox.confirm(
        props.drawerConfig.messageBox.message,
        props.drawerConfig.messageBox.title,
        {
          confirmButtonText: props.drawerConfig.messageBox.confirmButtonText,
          cancelButtonText: props.drawerConfig.messageBox.cancelButtonText,
          type: props.drawerConfig.messageBox.type,
        }
    )
        .then(() => {
          doit(props.drawerConfig.messageBox.confirmEventName, props.drawerConfig)
        })
        .catch(() => {
          doit('close')
        })
  } else {
    doit('close')
  }
}
//字典值转select值
const toDictSelect = (dictKey) => {
  return dictKeyToSelect(dictKey, props.dict.dictIndex, props.dict.dictList)
}

let treeName = ""
let treeId = ""
const selectTree = (formName, formId="id") => {
  treeName = formName
  treeId = formId
  console.log(formName, treeId)
}
const handleCheckClick = (e,treeName,treeId) => {
  console.log(e)
  console.log(treeName)
  console.log(form.value[treeName])
  toSelect(e,treeName,treeId)
}

function toSelect(e,treeName,treeId) {
  // console.log(">>>>>>", e, e.id, form.value[treeName].indexOf(e.id), e)
  console.log(e,'选中')
  if (e.children) { //有子元素则不进行判断
    for (let i in e.children) {
      // console.log(i, e[i])
      toSelect(e.children[i],treeName,treeId)
    }
  } else {
      form.value[treeName] = form.value[treeName]||[]
      let index = form.value[treeName].indexOf(e[treeId])
      if (index == -1) {
        if (e[treeId] != null) {
          form.value[treeName].push(e[treeId])
        }
      } else {
        form.value[treeName].splice(index, 1)
      }
  }
}
</script>

<template>

  <el-drawer
      v-model="drawerConfig.show"
      :title="drawerConfig.title"
      :direction="drawerConfig.direction"
      :before-close="handleClose"
      :show-close="true"
      :size="drawerConfig.size"
  >
    <div class="drawer-main">
<!--                  {{drawerData}}-->
      <el-form
          ref="ruleFormRef"
          :model="form"
          :rules="drawerConfig.rules"
          :label-width="drawerConfig.labelWidth"
          status-icon
      >
        <div
            v-for="(item, index) in drawerConfig.form"
            :key="index"
            :class="item.title?'isTitle':'isNotTitle'"
            :style="item.type=='hidden'?'':(item.width?'width:'+item.width+'%;':'width: 33%;')"
        >
          <div class="title">{{item.title}}</div>
          <div v-if="item.type!='hidden'&&!item.title">
            <el-alert type="info" show-icon :closable="false" v-if="displayAlert(item)">
              <p class="wos-font-sm">{{ item.alert }}</p>
            </el-alert>
            
            <!--文本框-->
            <el-form-item
                v-if="display(item, 'text')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-input v-if="drawerConfig.ifSave" v-model="form[item.key]" placeholder="请输入"/>
              <span v-else>{{ form[item.key] }}</span>
            </el-form-item>
  
            <!--时间-->
            <el-form-item
                v-if="display(item, 'date')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-config-provider :locale="locale">
                <el-date-picker
                    v-if="drawerConfig.ifSave"
                    v-model="form[item.key]"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="选择日期时间">
                </el-date-picker>
                <span v-else>{{ form[item.key] }}</span>
              </el-config-provider>
              
            </el-form-item>
  
            <!--密码框-->
            <el-form-item
                v-if="display(item, 'password')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-input type="password" v-if="drawerConfig.ifSave" v-model="form[item.key]"/>
              <span v-else>{{ form[item.key] }}</span>
            </el-form-item>
  
            <!--文本区域-->
            <el-form-item
                v-if="display(item, 'textarea')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-input
                  v-if="drawerConfig.ifSave"
                  v-model="form[item.key]"
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 4 }"
              />
              <span v-else>{{ form[item.key] }}</span>
            </el-form-item>
  
            <!--数字文本框-->
            <el-form-item
                v-if="display(item, 'number')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-input-number
                  v-if="drawerConfig.ifSave"
                  v-model="form[item.key]"
                  :min="item.min"
                  :max="item.max"
              />
              <span v-else>{{ form[item.key] }}</span>
            </el-form-item>
  
            <!--级联选择器-->
            <el-form-item
                v-else-if="display(item, 'treeSelect')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-tree-select
                  v-model="form[item.key]"
                  :data="item.options"
                  check-strictly
                  :render-after-expand="false"
                  :props="item.props"
                  placeholder="默认根目录"
                  class="wos-width-100"
                  :disabled="!drawerConfig.ifSave"
              />
            </el-form-item>
  
            <!--级联多项选择器-->
            <el-form-item
                v-else-if="display(item, 'treeSelects')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-tree-select
                  v-model="form[item.key]"
                  :data="item.options"
                  :render-after-expand="false"
                  :props="item.props"
                  placeholder="默认根目录"
                  class="wos-width-100"
                  :disabled="!drawerConfig.ifSave"
                  multiple
                  show-checkbox
              />
            </el-form-item>
  
            <!--树形菜单-->
            <el-form-item
                v-else-if="display(item, 'tree')"
                :label="item.label?item.label + ':':''"
                :label-width="item.label?'':'0px'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <div class="tree-area">
  <!--              {{ item.props.value }}-->
                <el-tree
                    :data="item.options"
                    :props="item.props"
                    :node-key="item.props.value"
                    :default-checked-keys="form[item.key]"
                    show-checkbox
                    @check="handleCheckClick($event,item.key, item.props.value)"
                    @check-change="selectTree(item.key, item.props.value)"
                />
              </div>
            </el-form-item>
  
            <!--单选按钮-->
            <el-form-item
                v-else-if="display(item, 'radio')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-radio-group v-model="form[item.key]" style="position:relative;top:-3px">
                <el-radio :disabled="!drawerConfig.ifSave" v-if="item.source == 'select'"
                          v-for="option in item.select" :label="option.value" size="large">
                  {{ option.label }}
                </el-radio>
  <!--  @ts-ignore-->
                <el-radio :disabled="!drawerConfig.ifSave" v-else v-for="option in toDictSelect(item.dict)"
                          :label="option['dictValue']" size="large">{{ option['dictLabel'] }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <!--单选按钮-->
            <el-form-item
                v-else-if="display(item, 'select')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
            <el-select v-model="form[item.key]" placeholder="请选择">
                  <el-option
                    v-for="dict in toDictSelect(item.dict)"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item 
                v-else-if="display(item, 'address')"
                :label="item.label + ':'"
                :prop="item.key"
                class="formItem"
                :class="drawerConfig.ifSave? '' : 'drawer-input'"
            >
              <el-cascader
                :options="provinceAndCityData"
                change-on-select
                placeholder="请选择"
                v-model="form[item.key]"
                expand-trigger="hover">
              </el-cascader>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <el-form>
        <div class="titleBox">
          <div class="title">简历</div>
          <div class="iconBox" @click="addJianli"><el-icon style="color:white;"><Plus /></el-icon></div>
        </div>
        <div v-for="(item,index) in jianli" :key="index" class="optionsBox">
          <el-form-item
            style="width: 300px;margin-right:20px"
          >
            <el-config-provider :locale="locale">
              <el-date-picker
                v-model="item.time"
                type="daterange"
                range-separator="—"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-config-provider>
          </el-form-item>
          <!--文本框-->
          <el-form-item style="width: 500px;margin-right:20px">
            <el-input v-model="item.edu" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请填写简介"/>
          </el-form-item>
          <el-button type="danger" plain @click="delJianli(index)">删除此条</el-button>
        </div>
      </el-form>
      <el-form>
        <div class="titleBox">
          <div class="title">奖惩情况</div>
          <div class="iconBox" @click="addJiangcheng"><el-icon style="color:white;"><Plus /></el-icon></div>
        </div>
        <div v-for="(item,index) in jiangcheng" :key="index" class="optionsBox">
          <el-form-item
            style="width: 300px;margin-right:20px"
          >
            <el-config-provider :locale="locale">
              <el-date-picker
                v-model="item.rewardTime"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-config-provider>
          </el-form-item>
          <!--文本框-->
          <el-form-item style="width: 500px;margin-right:20px">
            <el-input v-model="item.reward" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请填写具体内容"/>
          </el-form-item>
          <el-button type="danger" plain @click="delJiangcheng(index)">删除此条</el-button>
        </div>
      </el-form>
      <el-form>
        <div class="titleBox">
          <div class="title">家庭主要成员和社会关系</div>
          <div class="iconBox" @click="addFamily"><el-icon style="color:white;"><Plus /></el-icon></div>
        </div>
        <div v-for="(item,index) in family" :key="index" class="optionsBox">
          <!--文本框-->
          <el-form-item style="width: 80px;margin-right:20px">
            <el-input v-model="item.appellation"  placeholder="称谓"/>
          </el-form-item>
          <el-form-item style="width: 80px;margin-right:20px">
            <el-input v-model="item.name"  placeholder="姓名"/>
          </el-form-item>
          <el-form-item style="width: 80px;margin-right:20px">
            <el-input v-model="item.age"  placeholder="年龄"/>
          </el-form-item>
          <el-form-item style="width: 150px;margin-right:20px">
            <el-input v-model="item.outLook"  placeholder="政治面貌"/>
          </el-form-item>
          <el-form-item style="width: 400px;margin-right:20px">
            <el-input v-model="item.jobRemark" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="简介"/>
          </el-form-item>
          <el-button type="danger" plain @click="delFamily(index)">删除此条</el-button>
        </div>
      </el-form>
      <div v-if="drawerConfig.ifSave" style="padding-bottom:40px">
        <el-button type="primary" @click="doit('save', form,{peopleRewardList:jiangcheng,peopleResumeList:jianli,peopleMemberList:family})">
          保存
        </el-button>
        <el-button @click="doit('close')">放弃</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<style scoped lang="scss">
.formItem{
  
}
.isNotTitle{
  display: inline-block;
}
.title{
  font-size: 18px;
  margin: 20px 0;
  color: #888;
  font-weight: 700;
}
.titleBox{
  display: flex;
  align-items: center;
}
.iconBox{
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  line-height: 20px;
  justify-content: center;
  margin-left: 10px;
  background: #333;
}
.optionsBox{
  display: flex;
  align-items: flex-start;
  padding-bottom: 20px;
}
</style>